//
//  ZCycleView.m
//  05-Homework
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ZCycleView.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ZCycleView ()<UIScrollViewDelegate>
@property (nonatomic, strong) NSArray *imageArr;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UIScrollView *scrollView;
@end

@implementation ZCycleView

- (instancetype)initWithFrame:(CGRect)frame imageArr:(NSArray<UIImage *> *)imageArr {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatSubViewswithFrame:frame imageArr:imageArr];
    }
    return self;
}

- (void)creatSubViewswithFrame:(CGRect)frame imageArr:(NSArray<UIImage *> *)imageArr {
    self.imageArr = imageArr;
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(WIDTH * (imageArr.count + 2), 0);
    [self addSubview:_scrollView];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [_scrollView addSubview:scrollView];
    UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    firstImageView.image = _imageArr.lastObject;
    [scrollView addSubview:firstImageView];
    
    UIScrollView *lastScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * (imageArr.count - 1), 0, WIDTH, HEIGHT)];
    [_scrollView addSubview:lastScrollView];
    UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    lastImageView.image = _imageArr.firstObject;
    [lastScrollView addSubview:lastImageView];
    
    if (_imageArr.count > 0) {
        for (int i = 0; i < _imageArr.count; i++) {
            UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH + WIDTH * i, 0, WIDTH, HEIGHT)];
            smallScrollView.delegate = self;
            [self addSubview:smallScrollView];
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
            imageView.image = _imageArr[i];
            [smallScrollView addSubview:imageView];
        }
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH * 0.5, HEIGHT * 0.9, 50, 50)];
        _pageControl.numberOfPages = _imageArr.count;
        [self addSubview:_pageControl];
        [_pageControl addTarget:self action:@selector(pageValueChanged:) forControlEvents:UIControlEventValueChanged];
    }
}

- (void)pageValueChanged:(UIPageControl *)page {
    _scrollView.contentOffset = CGPointMake(page.currentPage * WIDTH, 0);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == WIDTH * 6) {
        [scrollView setContentOffset:CGPointMake(0, 0)];
    } else if (scrollView.contentOffset.x == 0) {
        [scrollView setContentOffset:CGPointMake(WIDTH * 5, 0)];
    }
    _pageControl.currentPage = scrollView.contentOffset.x / WIDTH - 1;
    
}

@end
